package cn.com.agree.order.service.impl;

import cn.com.agree.order.client.ProductClient;
import cn.com.agree.order.dao.OrderDetailDao;
import cn.com.agree.order.dao.OrderMasterDao;
import cn.com.agree.order.dto.CartDTO;
import cn.com.agree.order.dto.OrderDTO;
import cn.com.agree.order.entity.OrderDetail;
import cn.com.agree.order.entity.OrderMaster;
import cn.com.agree.order.entity.ProductInfo;
import cn.com.agree.order.enums.OrderStatusEnum;
import cn.com.agree.order.enums.PayStatusEnum;
import cn.com.agree.order.service.OrderService;
import cn.com.agree.order.utils.KeyUtil;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Descriptions...
 *
 * @author sun.ys
 * @date 2020/8/23.
 */
@Service
public class OrderServiceImpl implements OrderService {
    
    @Autowired
    private OrderDetailDao orderDetailDao;
    
    @Autowired
    private OrderMasterDao orderMasterDao;
    
    
    @Autowired
    private ProductClient  productClient;
    
    @Override
    public OrderDTO create(OrderDTO orderDTO) {
        
        String orderId = KeyUtil.genUniqueKey();
        
        //  查询商品信息（调用商品服务）
        List<String> productIdList = orderDTO.getOrderDetailList().stream()
                .map(OrderDetail::getProductId)
                .collect(Collectors.toList());
        List<ProductInfo> productInfoList = productClient.listForOrder(productIdList);
        
        //计算总价
        BigDecimal orderAmout = new BigDecimal(BigInteger.ZERO);
        for (OrderDetail orderDetail: orderDTO.getOrderDetailList()){
            
            for(ProductInfo  productInfo: productInfoList){
                if(productInfo.getProductId().equals(orderDetail.getProductId())){
                    //单价数量
                    orderAmout = productInfo.getProductPrice()
                            .multiply(new BigDecimal(orderDetail.getProductQuantity()))
                            .add(orderAmout);
                    
                    BeanUtils.copyProperties(productInfo,orderDetail);
                    orderDetail.setOrderId(orderId);
                    orderDetail.setDetailId(KeyUtil.genUniqueKey());
                    
                    //订单详情入库
                    orderDetailDao.save(orderDetail);
                    
                }
            }
            
            
        }
        
        
        
       
        //  扣库存（调用商品服务）
        List<CartDTO> cartDTOList = orderDTO.getOrderDetailList().stream()
                .map(e->new CartDTO(e.getProductId(),e.getProductQuantity()))
                .collect(Collectors.toList());
        
        productClient.decreaseStock(cartDTOList);
        
        
        
        
        //订单入库
        OrderMaster  orderMaster = new OrderMaster();
        orderDTO.setOrderId(orderId);
        BeanUtils.copyProperties(orderDTO,orderMaster);
        
        orderMaster.setOrderAmount(orderAmout);
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
       
        
        orderMasterDao.save(orderMaster);
        
        return orderDTO;
    }
}
